#include <pump.h>

#include "stdarg.h"

void Pump_init(Pump *pump, TIM_HandleTypeDef *htim, uint32_t channel) {
    PWM_init(&pump->pump_pwm, htim, channel);
    pump->is_on = 0;
}

void Pump_set(Pump *pump, _Bool on) {
    if (on) {
        PWM_set_full(&pump->pump_pwm);
        pump->is_on = 1;
    } else {
        PWM_set_zero(&pump->pump_pwm);
        pump->is_on = 0;
    }
}

void Pump_toggle(Pump *pump) { Pump_set(pump, !pump->is_on); }

void PumpGroup_init(PumpGroup *pump_group, Pump *pumps[], uint8_t pump_count) {
    pump_group->pumps = *pumps;
    pump_group->pump_count = pump_count;
}

void PumpGroup_set(PumpGroup *pump_group, uint8_t pump_index, _Bool on) {
    if (pump_index < pump_group->pump_count) {
        Pump_set(&pump_group->pumps[pump_index], on);
    }
}

void PumpGroup_set_all(PumpGroup *pump_group, _Bool on) {
    for (uint8_t i = 0; i < pump_group->pump_count; i++) {
        Pump_set(&pump_group->pumps[i], on);
    }
}

void PumpGroup_toggle(PumpGroup *pump_group, uint8_t pump_index) {
    if (pump_index < pump_group->pump_count) {
        Pump_toggle(&pump_group->pumps[pump_index]);
    }
}

void PumpGroup_toggle_all(PumpGroup *pump_group) {
    for (uint8_t i = 0; i < pump_group->pump_count; i++) {
        Pump_toggle(&pump_group->pumps[i]);
    }
}
